﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using AForge.Video.DirectShow;
using Sunny.UI;

namespace TestProject
{
    public partial class frmImageOperate : UIPage
    {
        //图片路径
        string curFileName = string.Empty;

        public frmImageOperate()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取路径
        /// </summary>
        /// <returns></returns>
        private string GetPath()
        {
            var filePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + Path.DirectorySeparatorChar.ToString();

            //判断该路径是否存在 不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            return filePath;
        }

        #region 选择图片
        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SelectImage1_Click(object sender, EventArgs e)
        {
            Bitmap curBitmap;
            //创建OpenFileDialog
            OpenFileDialog opnDlg = new OpenFileDialog();
            //为图像选择一个筛选器
            opnDlg.Filter = "所有图像文件|*.bmp;*.pcx;*.png;*.jpg;*.gif;" +
                "*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" +
                "位图(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" +
                "矢量图(*.wmf;*.eps;*.emf;..)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf";
            //设置对话框标题
            opnDlg.Title = "打开图像文件";
            //启用帮助按钮
            opnDlg.ShowHelp = true;
            //不可选择多个图片文件
            opnDlg.Multiselect = false;
            //如果选择的结果为打开，则选定文件
            if (opnDlg.ShowDialog() == DialogResult.OK && opnDlg.FileName != string.Empty)
            {
                //读取当前选中的文件地址
                curFileName = opnDlg.FileName;
                //Image image = Image.FromFile(curFileName);
                this.pictureBox1.Load(curFileName);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//加载的图片自适应picrurebox大小
            }
            try
            {
                //使用 Image.FromFile创建图像对象
                curBitmap = (Bitmap)Image.FromFile(curFileName);
            }
            catch (Exception ex)
            {
                //显示异常；
                MessageBox.Show(ex.Message);
            }
            Invalidate();
        }
        #endregion

        #region 转PDF
        /// <summary>
        /// 转PDF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ToPdf1_Click(object sender, EventArgs e)
        {
            #region 配置文件内容
            saveFileDialog1.Filter = "*.pdf|Pdf文件";//设置文件类型
            saveFileDialog1.DefaultExt = ".pdf";//默认文件的拓展名
            saveFileDialog1.FileName = "转换后的文件";//文件名
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            #endregion
            //最终压缩后要保存的路径
            string pdfFilePath = this.saveFileDialog1.FileName;

            ImageToPDF(curFileName, pdfFilePath);

            ShowSuccessTip(@"转换完成"); 

            //打开资源管理器并选中文件
            Process.Start("explorer.exe", "/select," + pdfFilePath);
        }

        /// <summary>
        /// 图片转PDF
        /// </summary>
        /// <param name="jpgfile">图片本地地址</param>
        /// <param name="pdf">生成的文件路径和文件名</param>
        /// <returns></returns>
        public static bool ImageToPDF(string jpgfile, string pdf)
        {
            //判断该路径是否存在 不存在则创建
            if (!Directory.Exists(Path.GetDirectoryName(pdf)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(pdf));
            }
            var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
            using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                //这里加try catch 是因为这个地方我用的时候会报错 但是不影响使用 
                try
                {
                    iTextSharp.text.pdf.PdfWriter.GetInstance(document, stream);
                }
                catch (Exception ex)
                {
                    //LogHelper.WriteLog("[错误消息]:" + ex.Message + "\r\n" + "[错误文件]:" + ex.Source + "\r\n" + "[错误方法]:" + ex.TargetSite + "\r\n" + "[堆栈消息]:" + ex.StackTrace);
                }
                document.Open();
                using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    var image = iTextSharp.text.Image.GetInstance(imageStream);
                    if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
                    {
                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                    }
                    else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
                    {
                        image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                    }
                    image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
                    document.Add(image);
                }
                document.Close();
            }
            return true;
        }
        #endregion

        #region 上传图片
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }
        #endregion

        #region 下载图片
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            ShowWarningTip("暂未实现");
        }
        #endregion
    }
}
